# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 3
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''


class InvoiceAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()  # 初始化数据库链接

    def queryInvoice(self, page_index):  # zzh_prochild表查询记录,根据什么规则查询？
        cur = self.conn.cursor()
        sql = "select * from zzh_invoice"
        total_count = cur.execute(sql)
        invoiceList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            invoiceListPart = invoiceList[start: end]

            invoiceListJson = Table2Json.invoice_list_to_json(invoiceListPart)
            return total_count, invoiceListJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            invoiceListPart = invoiceList[start:]
            invoiceListJson = Table2Json.invoice_list_to_json(invoiceListPart)
            return total_count, invoiceListJson

    def addInvoice(self, addInvoice):  # process表添加记录
        invoiceType = addInvoice['invoiceType']  # invoiceType  发票类型
        invoiceContent = addInvoice['invoiceContent']  # processChildCode 发票内容
        invoiceHeader = addInvoice['invoiceHeader']  # invoiceHeader  发票抬头
        invoiceStatus = addInvoice['invoiceStatus']  # invoiceStatus  出票状态：0：默认，未出票；1：已出票
        invoiceRemark = addInvoice['invoiceRemark']
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_invoice (invoice_type,invoice_content,invoice_header,invoice_status,invoice_remark) values (%s,%s,%s,%s,%s)"
        addResultOfDatabaseInvoice = cur.execute(sql, [invoiceType, invoiceContent, invoiceHeader, invoiceStatus,
                                                       invoiceRemark])
        self.conn.commit()
        if addResultOfDatabaseInvoice:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateInvoiceByInvoiceId(self, newInvoice, invoiceId):
        invoiceType = newInvoice['invoiceType']  # processChildName  子流程的名称
        invoiceContent = newInvoice['invoiceContent']  # processChildCode 子流程步数
        invoiceHeader = newInvoice['invoiceHeader']  # processChildStatus  子流程状态
        invoiceStatus = newInvoice['invoiceStatus']
        invoiceRemark = newInvoice['invoiceRemark']

        cur = self.conn.cursor()
        sql = "UPDATE zzh_invoice SET invoice_type=%s,invoice_content=%s,invoice_header=%s,invoice_status=%s,invoice_remark=%s WHERE invoice_id=%s"
        updateInvoiceResultOfDatabase = cur.execute(sql, [invoiceType, invoiceContent, invoiceHeader, invoiceStatus,invoiceRemark,
                                                          invoiceId])
        self.conn.commit()
        if updateInvoiceResultOfDatabase:  # 更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteInvoiceById(self, invoiceId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_invoice WHERE invoice_id=%s"
        deleteInvoiceResultOfDatabase = cur.execute(sql, [invoiceId])
        self.conn.commit()
        if deleteInvoiceResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0
